#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h> //交叉编译器中的头文件，不是内核的
#include <signal.h>
#include <fcntl.h>


struct key_report{
	int code;
	int value;
};

int fd;
struct key_report event;

void key_drv_sighandler(int signo)
{
	read(fd, &event, sizeof(event));
	switch(event.code)
	{
		case KEY_A:
		{
			if(event.value == 0)
				printf("KEY_A----->pressed\n");
			else
				printf("KEY_A----->released\n");
		}
			break;
		case KEY_B:
		{
			if(event.value == 0)
				printf("KEY_B----->pressed\n");
			else
				printf("KEY_B----->released\n");
		}
			break;
		case KEY_C:
		{
			if(event.value == 0)
				printf("KEY_C----->pressed\n");
			else
				printf("KEY_C----->released\n");
		}
			break;
		default:
			break;
	}

}


int main(void)
{
	unsigned int flags;
	fd = open("/dev/key_drv",O_RDWR);
	if(fd < 0)
	{
		perror("open");
		exit(1);
	}
	//注册信号
	signal(SIGIO, key_drv_sighandler);
	//告诉驱动，信号应该发给谁
	fcntl(fd, F_SETOWN, getpid());
	//设置为异步通知模式
	flags = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flags|FASYNC);

	while(1)
	{
		printf("I am daoge...\n");
		sleep(2);
	}
	close(fd);
	return 0;
}
